package com.entfrm.biz.it.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entfrm.biz.it.entity.Repair;
import com.entfrm.biz.it.manager.RepairManager;
import com.entfrm.biz.it.service.RepairService;
import com.entfrm.core.base.constant.CommonConstants;
import com.entfrm.core.log.annotation.OperLog;
import com.entfrm.core.security.util.SecurityUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import com.entfrm.core.base.api.R;
import com.entfrm.core.base.util.ExcelUtil;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.security.Security;
import java.util.List;

/**
 * @author kyc
 * @date 2020-08-11 13:45:40
 * @description 报修单Controller
 */
@Api("报修单管理")
@RestController
@AllArgsConstructor
@RequestMapping("/it/repair")
public class RepairController {

    private final RepairService repairService;

    private final RepairManager repairManager;

    private QueryWrapper<Repair> getQueryWrapper(Repair repair) {
        return new QueryWrapper<Repair>()
                .like(StrUtil.isNotBlank(repair.getName()), "name", repair.getName())
                .eq(StrUtil.isNotBlank(repair.getJobId()), "job_id", repair.getJobId())
                .eq(StrUtil.isNotBlank(repair.getPhone()), "phone", repair.getPhone())
                .like(StrUtil.isNotBlank(repair.getServiceName()), "service_name", repair.getServiceName())
                .eq(StrUtil.isNotBlank(repair.getLocation()), "location", repair.getLocation())
                .eq(StrUtil.isNotBlank(repair.getProblemInfo()), "problem_info", repair.getProblemInfo())
                .eq(StrUtil.isNotBlank(repair.getEmergency()), "emergency", repair.getEmergency())
//                .between(StrUtil.isNotBlank(repairDocument.getAssignTime()), "assign_time", repairDocument.getAssignTime())
//                .between(StrUtil.isNotBlank(repairDocument.getResolveTime()), "resolve_time", repairDocument.getResolveTime())
                .eq(StrUtil.isNotBlank(repair.getStatus()), "status", repair.getStatus())
                .eq(StrUtil.isNotBlank(repair.getPhoto()), "photo", repair.getPhoto())
                .between(StrUtil.isNotBlank(repair.getBeginTime()) && StrUtil.isNotBlank(repair.getEndTime()), "create_time", repair.getBeginTime(), repair.getEndTime())
                .orderByDesc("create_time");
    }

    @ApiOperation("报修单列表")
    @PreAuthorize("@ps.hasPerm('repair_view')")
    @GetMapping("/list")
    public R list(Page page, Repair repair) {
        IPage<Repair> repairPage = repairService.page(page, getQueryWrapper(repair));
        return R.ok(repairPage.getRecords(), repairPage.getTotal());
    }

    @ApiOperation("我的报修单列表")
    @PreAuthorize("@ps.hasPerm('repair_my_repairs')")
    @GetMapping("/listMyRepair")
    public R listMyRepair(Page page, Repair repair) {
        IPage<Repair> repairPage = repairService.page(page, getQueryWrapper(repair).lambda().eq(Repair::getCreateBy, SecurityUtil.getUser().getUsername()));
        return R.ok(repairPage.getRecords(), repairPage.getTotal());
    }

    @ApiOperation("我的维修工单列表")
    @PreAuthorize("@ps.hasPerm('repair_my_services')")
    @GetMapping("/listMyService")
    public R listMyService(Page page, Repair repair) {
        IPage<Repair> repairPage = repairService.page(page, getQueryWrapper(repair).lambda().eq(Repair::getServiceId, SecurityUtil.getUser().getId()));
        return R.ok(repairPage.getRecords(), repairPage.getTotal());
    }

    @ApiOperation("报修单查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        Repair repair = repairManager.getRepairById(id);
        return R.ok(repair);
    }

    @OperLog("报修单新增")
    @ApiOperation("报修单新增")
    @PreAuthorize("@ps.hasPerm('repair_add')")
    @PostMapping("/save")
    public R save(@Validated @RequestBody Repair repair) {
        repairManager.saveRepair(repair);
        return R.ok();
    }

    @OperLog("报修单修改")
    @ApiOperation("报修单修改")
    @PreAuthorize("@ps.hasPerm('repair_edit')")
    @PutMapping("/update")
    public R update(@Validated @RequestBody Repair repair) {
        if (!repair.getStatus().equals(CommonConstants.RepairStatus.WAIT_ASSIGN)){
            return R.error("该状态不允许修改!");
        }
        repairService.updateById(repair);
        return R.ok();
    }

    @OperLog("报修单指派")
    @ApiOperation("报修单指派")
    @PreAuthorize("@ps.hasPerm('repair_assign')")
    @PutMapping("/repairAssign")
    public R repairAssign(@Validated @RequestBody Repair repair) {
        R result = repairManager.repairAssign(repair);
        return result;
    }

    @OperLog("报修单解决回复")
    @ApiOperation("报修单解决回复")
    @PreAuthorize("@ps.hasPerm('repair_resolved')")
    @PutMapping("/repairResolved")
    public R repairResolved(@Validated @RequestBody Repair repair) {
        R result = repairManager.repairResolved(repair);
        return result;
    }

    @OperLog("报修单评价")
    @ApiOperation("报修单评价")
    @PreAuthorize("@ps.hasPerm('repair_comment')")
    @PutMapping("/repairComment")
    public R repairComment(@Validated @RequestBody Repair repair) {
        R result = repairManager.repairComment(repair);
        return result;
    }

    @OperLog("报修单删除")
    @ApiOperation("报修单删除")
    @PreAuthorize("@ps.hasPerm('repair_del')")
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable("id") Integer[] ids) {
        return repairManager.deleteRepair(ids);
    }


    @PreAuthorize("@ps.hasPerm('repair_export')")
    @GetMapping("/export")
    public R export(Repair repair) {
        List<Repair> list = repairService.list(getQueryWrapper(repair));
        ExcelUtil<Repair> util = new ExcelUtil<Repair>(Repair.class);
        return util.exportExcel(list, "报修单数据");
    }
}
